from utils.query import *
from utils.getPublicData import *
from collections import defaultdict
from foodApp.models import *

def getIndexData():
    fooddataList = list(getfooddata())
    max_value = 0
    max_name = ""

    for i in fooddataList:
        value = i[7]
        name = i[0]
        if value > max_value:
            max_value = value
            max_name =name
    print(max_name, max_value)

    max_comment = max([x[5] for x in fooddataList])

    name_count = defaultdict(int)
    for i in fooddataList:
        name = i[2]
        name_count[name] += 1

    most_name = max(name_count,key=name_count.get)
    print(max_comment,most_name)

    return max_value,max_name,max_comment,most_name

def getIndexChartData():
    maxPriceTop = list(getmaxPriceTop())
    print(maxPriceTop)
    xBarData = [x[0] for x in maxPriceTop]
    y1BarData = [x[1] for x in maxPriceTop]
    y2BarData = [x[2] for x in maxPriceTop]

    typeCountList = list(gettypeCount())
    print(typeCountList[0])
    cityAvgList = list(getcityAvg())
    print(cityAvgList[0])
    pieData = []
    for i in typeCountList:
        pieData.append({
            'name':i[0],
            'value':i[1]
        })
    funnelData = []
    colorList = ['#FDD56A','#FDB36A','pink','#73ACFF','#FD866A','#FDD56A','#FDB36A','pink','#73ACFF','#FD866A','#FDD56A','#FDB36A','pink','#73ACFF','#FD866A','#FDD56A','#FDB36A','pink','#73ACFF','#FD866A','#FDD56A','#FDB36A','pink','#73ACFF','#FD866A']
    for index,value in enumerate(cityAvgList):
        funnelData.append({
            'name':value[0],
            'value':round(value[1],0),
            'itemStyle':{
                'color':colorList[index]
            }
        })
    print(pieData,funnelData)
    return xBarData,y1BarData,y2BarData,pieData,funnelData

def changePwd(passwordInfo,userInfo):
    oldPwd = passwordInfo['oldPwd']
    newPwd = passwordInfo['newPwd']
    ckdPwd = passwordInfo['ckdPwd']

    user = User.objects.get(username=userInfo.username)
    if user.password != oldPwd:
        return '原密码输入错误！请重新输入'
    else:
        if newPwd != ckdPwd:
            return '两次密码不一致'
        else:
            user.password = newPwd
            user.save()


def getTypeChartData():
    typeRateList = list(gettypeRate())
    lineXData = [x[0] for x in typeRateList]
    lineY1Data = [round(x[1],3) for x in typeRateList]
    lineY2Data = [round(x[2],3) for x in typeRateList]
    lineY3Data = [round(x[3],3) for x in typeRateList]

    specificType = list(getspecificType())
    pieData = []
    for i in specificType:
        pieData.append({
            'name':i[0],
            'value':i[1]
        })

    typeCommentList = list(gettypeComment())
    barXData = [x[0] for x in typeCommentList]
    barYData = [round(x[1],1) for x in typeCommentList]
    return lineXData,lineY1Data,lineY2Data,lineY3Data,pieData[:15],barXData,barYData

def getPriceChartData():
    maxPriceCityList = list(getmaxPriceCity())
    priceCategoryList = list(getcategoryPrice())
    typePriceList = list(gettypePrice())

    leftXData = [x[0] for x in maxPriceCityList]
    leftYData = [x[1] for x in maxPriceCityList]

    rightXData = [x[0] for x in typePriceList]
    rightYData = [round(x[1],1) for x in typePriceList]

    circleData = []
    for i in priceCategoryList:
        circleData.append({
            'name':i[0],
            'value':i[1]
        })
    return leftXData,leftYData,rightXData,rightYData,circleData

def getRateChartData():
    startCountList = list(getstartCount())
    mixrateAvgList = list(getmixrateAvg())
    def map_fn(item):
        item_list = list(item)
        item_list[0] = item_list[0]/10
        return item_list
    startCountList = map(map_fn,startCountList)
    roseData = []
    for i in startCountList:
        roseData.append({
            'name':i[0],
            'value':i[1]
        })
    xData = [x[0] for x in mixrateAvgList]
    yData = [round(x[1],3) for x in mixrateAvgList]
    realData = []
    for i in startCountList:
        realData.append({
            'areaName':i[0],
            'rate': i[1]
        })
    return xData,yData,roseData,realData

def getAreaChartData(defaultNum):
    mamCityList = list(getmamCity())
    hotAddressList = list(gethotAddress())
    xDate = [x[0] for x in hotAddressList]
    yData = [x[1] for x in hotAddressList]
    mapData = []
    if defaultNum == '最大值':
        for i in mamCityList:
            mapData.append({
                'name': i[0],
                'value': i[1]
            })
    elif defaultNum == '平均值':
        for i in mamCityList:
            mapData.append({
                'name': i[0],
                'value': i[2]
            })
    elif defaultNum == '最小值':
        for i in mamCityList:
            mapData.append({
                'name': i[0],
                'value': i[3]
            })
    return mapData,xDate[::-1],yData[::-1]

def getPreSelect():
    fooddataList = list(getfooddata())
    typeList = [x[2] for x in fooddataList]
    return typeList